package org.fujene.sandbox;

import java.io.IOException;
import java.net.UnknownHostException;

import org.junit.Test;
import org.msgpack.annotation.MessagePackMessage;
import org.msgpack.rpc.Client;
import org.msgpack.rpc.Server;
import org.msgpack.rpc.loop.EventLoop;

public class TestMsgpack{
	@MessagePackMessage
	public class Structure{
		public int val;
		public String str;
	}
	
	public class TestServerHandler{
		public int[] sender(int start, int end){
			int[] returner = new int[end - start + 1];
			for(int i = 0; i < returner.length; i++){
				returner[i] = start + i;
			}

			return returner;
		}
		
		public Structure[] sendclasses(int start, int end) {
			Structure[] srcs = new Structure[end - start + 1];
			
			for(int i = 0; i < srcs.length; i++){
				srcs[i] = new Structure();
				srcs[i].val = start + i;
				srcs[i].str = Integer.toString(i);
			}
			
			return srcs;
		}
	}

	public interface TestHandlerInterface{
		public int[] sender(int start, int end);
		
		public Structure[] sendclasses(int start, int end);
	}

	@Test
	public void recieveArray(){
		EventLoop loop = EventLoop.defaultEventLoop();
		Server svr = new Server(loop);

		svr.serve(new TestServerHandler());
		try{
			svr.listen(44555);
		}catch(IOException e){}
		
		try{
			Client client = new Client("localhost", 44555, loop);
			TestHandlerInterface ifce = client.proxy(TestHandlerInterface.class);
			Structure[] srcs = ifce.sendclasses(40, 50);
			for(int i = 0; i < srcs.length; i++){
				System.out.println(srcs[i].str);
			}
		}catch(UnknownHostException e){
		}
	}
}
